﻿namespace groopServer.Web {
    using System.Web;
    using System.Web.Mvc;
    using Common;
    using groopServer.Common;
    using groopServer.Common.Extensions;
    using groopServer.Common.Logging;
    using Common.Impl;
    using MvcContrib.Castle;

    public class GroopApplication : HttpApplication {
        protected void Application_Start() {
            SetupLogging();
            RegisterCommonTypes();
            RegisterMvcTypes();

            SetupControllerBuilder();
            SetupRoutes();
        }

        private static void RegisterCommonTypes() {
            IoC.Register<ILogger, Logger>("default-logger");
            IoC.Register<IRouteConfigurator, RouteConfigurator>("route-configurator");
            IoC.Register<IRouteConfigurator, IIS6RouteConfigurator>("route-configurator-iis6");
        }

        private static void RegisterMvcTypes() {
            var registration = IoC.Resolve<IExtensionRegistration>("registration-controllers");
            registration.Register();
        }

        private static void SetupRoutes() {
            var iis7configurator = IoC.Resolve<IRouteConfigurator>("route-configurator");
            var iis6configurator = IoC.Resolve<IRouteConfigurator>("route-configurator-iis6");

            iis7configurator.RegisterRoutes();
            iis6configurator.RegisterRoutes();
        }

        private static void SetupLogging() {
            LoggingUtility.Initialize();
        }

        private static void SetupControllerBuilder() {
            var controllerFactory = new WindsorControllerFactory(IoC.GetContainer());
            ControllerBuilder.Current.SetControllerFactory(controllerFactory);
        }
    }
}